import { apiBaseUrl, imgBaseUrl, defaultImg } from "../../../utils/config";
import { collectFormIds, saveFormIds, getOpenId, promiseRequest } from "../../../utils/index";
const app = getApp();
var formIdlist = [];
Page({
  data: {
    actshareinfo:{},
    ljtj:'',
    isshare:false,
    acthelpusers:[],
    hashelp:false,
    actreceive:{},
    lqropenid:'',
    actId:'',
    backImg: '',
    shareTitle: ''
  },
  onLoad(options) {
  //获取到领取人的openid
  this.data.lqropenid = options.lqropenid;
  this.data.actId = options.actId;
  //查询活动信息
    //getToken().then(() => {
      //查询活动信息
      this.getactshareinfo(this.data.actId);
   // });
  

},
onShow(){
  //查询领取人的领取活动信息
  this.getactreceive(this.data.lqropenid);
},
//页面销毁时提交收集到的formid
onUnload() {
  saveFormIds(formIdlist);
  formIdlist = [];
},
getactshareinfo(actId){
  let requestObj = {
    url: '/wxa/actshare/getcacheShareById',
    method: 'post',
    dataobj: {actId: actId },
  };
  promiseRequest(requestObj).then((res) => {
    if (res.code == 200) {
      let actshareinfo = res.data;
      let backImg = '';
      if (actshareinfo.backImg == '' || actshareinfo.backImg == null) {
        backImg = defaultImg
      } else {
        backImg = imgBaseUrl + "/" + actshareinfo.backImg
      }
      this.setData({
        actshareinfo: actshareinfo,
        backImg: backImg,
        shareTitle: "免费领取" + actshareinfo.rewardName + "(价值:" + actshareinfo.rewardPrice + "元)"
      })
      //助力个数
      let helpNumber = actshareinfo.helpNumber ? actshareinfo.helpNumber : 'X';
      if (actshareinfo.helpWayCode == 0) {
        this.setData({
          ljtj: '邀请好友助力达' + helpNumber + '次即可领奖',
        })
      } else if (actshareinfo.helpWayCode == 1) {//助力前N名
        this.setData({
          ljtj: '助力排名前' + helpNumber + '即可领奖',
        })
      }
    }
  }).catch((errMsg) => {
  });
},
share(){
  this.setData({
    isshare: true
  })
},
onShareAppMessage() {
  return {
    title: this.data.shareTitle,
    path: '/pages/compent/shareReceive/shareReceive?lqropenid='+this.data.lqropenid+'&actId=' + this.data.actId,
    imageUrl: this.data.backImg
  }
},
hideModal(){
  this.setData({
    isshare: false
  })
},
jump(e) {
  var page = e.currentTarget.dataset.page;
  wx.navigateTo({
    url: page
  });
},
//根据领取人的openid和当前活动id actId查询用户领取信息
getactreceive(lqropenid) {
  let requestObj = {
    url: '/wxa/actreceive/detailCache',
    method: 'post',
    dataobj: { actId: this.data.actId, openid: lqropenid },
  };
  promiseRequest(requestObj).then((res) => {
    if (res.code == 200) {
      this.setData({
        actreceive: res.data
      })
      //查询好友助力信息
      this.gethelpusers(this.data.actreceive.id);
    }
  }).catch((errMsg) => {
  });
},
//根据receiveId查询用户助力信息
gethelpusers(receiveId) {
  let openid = getOpenId();
  let requestObj = {
    url: '/wxa/acthelpuser/queryhelpusers',
    method: 'post',
    dataobj: { receiveId: receiveId },
  };
  promiseRequest(requestObj).then((res) => {
    if (res.code == 200) {
      //判断当前登录人是否已经参加过助力
      let hashelp = false;
      let acthelpusers = res.data.data;
      this.setData({
        acthelpusers: acthelpusers
      })
      for (var i = 0; i < acthelpusers.length; i++) {
        if (acthelpusers[i].openid == openid) {
          hashelp = true;
          break;
        }
      }
      this.setData({
        hashelp: hashelp
      })
    }
  }).catch((errMsg) => {
  });
},
//助他一臂之力
help(param){
  let helper = {}; 
  helper.nickName = param.nickName;
  helper.avatarUrl = param.avatarUrl;
  let requestObj = {
    url: '/wxa/acthelpuser/save',
    method: 'post',
    dataobj: param,
  };
  promiseRequest(requestObj).then((res) => {
    if (res.code == 200) {
      this.setData({
        hashelp: true,
        acthelpusers: this.data.acthelpusers.concat(helper)
      })
    }
  }).catch((errMsg) => {
  });
},
//用户参加活动需获取用户信息
getUserInfo(e){
  if (e.detail.userInfo){
    let userinfo = e.detail.userInfo;
    let receiveId = this.data.actreceive.id;
    let lqropenid = this.data.actreceive.openid;
    let openid = getOpenId();
    let param = { actId: this.data.actId, lqropenid: lqropenid, openid: openid, receiveId:receiveId,nickName: userinfo.nickName, avatarUrl: userinfo.avatarUrl, gender: userinfo.gender, language: userinfo.language }
    //用户按了允许授权按钮
    wx.checkSession({
      success: () => {
        this.help(param);
      },
      fail: () => {
        // 重新授权
        // getToken().then(() => {
        // this.help(param);
        // })
      }
    })
  } else {
    console.log("拒绝决绝");
  }
},
//我也要领取
myreceive(){
  wx.navigateTo({
    url: '../shareHelp/shareHelp?actId='+ this.data.actId
  });
},
//助他一臂之力收集fomid
ztybzl(e){
  console.log('单击助他一臂之力收集fomid');
  collectFormIds(e, formIdlist);
}
})